﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Controls.Menu
{
    public partial class MenuItem : UserControl
    {

        public MenuItem()
        {
            InitializeComponent();
            this.label1.Click += On_Click;
            this.pictureBox1.Click += On_Click;
            this.flowLayoutPanel1.Click += On_Click;

            this.label1.MouseMove += On_MouseMove;
            this.pictureBox1.MouseMove += On_MouseMove;
            this.flowLayoutPanel1.MouseMove += On_MouseMove;

            this.label1.MouseLeave += On_MouseLeave;
            this.pictureBox1.MouseLeave += On_MouseLeave;
            this.flowLayoutPanel1.MouseLeave += On_MouseLeave;

            this.Click += MenuItem_Click;
            this.MouseMove += MenuItem_MouseMove;
            this.MouseLeave += MenuItem_MouseLeave;
            


            FlagActive = true;
        }

       
        private void SetToolTip()
        {
            this.toolTip1.SetToolTip(flowLayoutPanel1, ToolTipText);
            this.toolTip1.SetToolTip(label1, ToolTipText);
            this.toolTip1.SetToolTip(pictureBox1, ToolTipText);
        }

        private void On_MouseLeave(object sender, EventArgs e)
        {
            OnMouseLeave(null);
        }

        private void On_MouseMove(object sender, MouseEventArgs e)
        {
            OnMouseMove(null);
        }

        private void MenuItem_MouseLeave(object sender, EventArgs e)
        {
            this.flowLayoutPanel1.BackColor = SystemColors.ControlLight;
        }

        private void MenuItem_MouseMove(object sender, MouseEventArgs e)
        {
            this.flowLayoutPanel1.BackColor = Color.White;
        }
        
   

        public bool IsRadio { get; set; }

        private void MenuItem_Click(object sender, EventArgs e)
        {
            if (IsRadio)
            {
                FlagActive = true;
            }
            else
            {
                FlagActive = !FlagActive;
            }
        }

     

        private void On_Click(object sender, EventArgs e)
        {
            this.OnClick(null);
        }
        
        public string TitleName
        {
            get { return this.label1.Text; }
            set { this.label1.Text = value; }
        }

        [Browsable(true)]
        [Description("设置控件图片"), Category("setPic"), DefaultValue(" ")]
        public Bitmap ImageIcon
        {
            get { return (Bitmap)this.pictureBox1.Image; }
            set { this.pictureBox1.Image = value; }
        }
        public Bitmap ImageActive { get; set; }
        public Bitmap ImageUnActive { get; set; }

        public string TitleActive { get; set; }
        public string TitleUnActive { get; set; }
        private bool _flagActive;
        private string _toolTipText;

        public bool FlagActive
        {
            get { return _flagActive; }
            set
            {
                _flagActive = value;
                if (_flagActive)
                {
                    ImageIcon = ImageActive;
                    TitleName = TitleActive;
                    OnChecked?.Invoke(this,new EventArgs());
                }
                else
                {
                    ImageIcon = ImageUnActive;
                    TitleName = TitleUnActive;
                    OnUnchecked?.Invoke(this, new EventArgs());
                }

            }
        }

        public event EventHandler<EventArgs> OnChecked;
        public event EventHandler<EventArgs> OnUnchecked;

        private void flowLayoutPanel1_AutoSizeChanged(object sender, EventArgs e)
        {
            this.Size = flowLayoutPanel1.Size;
        }

        public string ToolTipText
        {
            get { return _toolTipText; }
            set
            {
                if (_toolTipText == value)
                    return;

                _toolTipText = value;
                SetToolTip();
            }
        }
    }

}
